package example.json.msg.impl;

import example.json.msg.MsgBaseService;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import static example.json.msg.conts.MsgConsts.*;
import static example.json.msg.util.PhoneNumberUtil.phone;
import static example.json.msg.util.PhoneNumberUtil.sendtime;
import static example.json.msg.util.PhoneNumberUtil.tommorrow;

@SuppressWarnings("all")
@Repository
@Data
@Setter
@Getter
public class OnlineExample extends BaseHelper implements MsgBaseService {

    @Override
    public String batchSend(String accType, String phones, String content) {
        String online_acc, online_pwd;
        //转换行业类营销类短信
        switch(accType){
            case YX:
                online_acc = online_yx_acc;
                online_pwd = online_yx_pwd;
                break;
            default:
                online_acc = online_hy_acc;
                online_pwd = online_hy_pwd;
                break;
        }

        JSONObject odata = new JSONObject();
        odata.put("userId", online_acc);
        odata.put("md5password", online_pwd);
        odata.put("msgId", "12345");
        odata.put("extCode", "12345");
        odata.put("action", "sendsms");

        if (StringUtils.isNotEmpty(phones)) {
            odata.put("mobile", phones);
        } else {
            odata.put("mobile", phone(1));
        }

        odata.put("content" , content);

        return httpClient(online_master_url + online_batch_send_sub_url)
                .doPostParams(odata);
    }

    @Override
    public String mutipleSend(String accType, String content) {
        String online_acc, online_pwd;
        //转换行业类营销类短信
        switch(accType){
            case YX:
                online_acc = online_yx_acc;
                online_pwd = online_yx_pwd;
                break;
            default:
                online_acc = online_hy_acc;
                online_pwd = online_hy_pwd;
                break;
        }

        JSONObject odata = new JSONObject();
        odata.put("userId", online_acc);
        odata.put("md5password", online_pwd);
        odata.put("msgId", "12345");
        odata.put("extCode", "12345");
        if (StringUtils.isNotEmpty(content)) {
            odata.put("contentArr", online_batch_request_body(content));
        } else {
            odata.put("contentArr", online_batch_request_body());
        }

        return httpClient(online_master_url + online_mutiple_send_sub_url)
                .doPostParams(odata);
    }

    @Override
    public String voice(String accType, String phones, String content) {
        return null;
    }

    @Override
    public String report(String accType) {
        JSONObject odata = new JSONObject();
        String online_acc, online_pwd;
        //转换行业类营销类短信
        switch(accType){
            case YX:
                online_acc = online_yx_acc;
                online_pwd = online_yx_pwd;
                break;
            default:
                online_acc = online_hy_acc;
                online_pwd = online_hy_pwd;
                break;
        }
        odata.put("userId", online_acc);
        odata.put("md5password", online_pwd);
        odata.put("msgId", "12345");
        odata.put("extCode", "12345");
        odata.put("action", "getsendreport");

        return httpClient(online_master_url + online_batch_send_sub_url)
                .doPostParams(odata);
    }

    @Override
    public String sms(String accType) {
        JSONObject odata = new JSONObject();
        String online_acc, online_pwd;
        //转换行业类营销类短信
        switch(accType){
            case YX:
                online_acc = online_yx_acc;
                online_pwd = online_yx_pwd;
                break;
            default:
                online_acc = online_hy_acc;
                online_pwd = online_hy_pwd;
                break;
        }
        odata.put("userId", online_acc);
        odata.put("md5password", online_pwd);
        odata.put("msgId", "12345");
        odata.put("extCode", "12345");
        odata.put("action", "getdeliver");

        return httpClient(online_master_url + online_batch_send_sub_url)
                .doPostParams(odata);
    }

    @Override
    public String balance(String accType) {
        JSONObject odata = new JSONObject();
        String online_acc, online_pwd;
        //转换行业类营销类短信
        switch(accType){
            case YX:
                online_acc = online_yx_acc;
                online_pwd = online_yx_pwd;
                break;
            default:
                online_acc = online_hy_acc;
                online_pwd = online_hy_pwd;
                break;
        }
        odata.put("userId", online_acc);
        odata.put("md5password", online_pwd);
        odata.put("msgId", "12345");
        odata.put("extCode", "12345");
        odata.put("action", "getbalance");

        return httpClient(online_master_url + online_batch_send_sub_url)
                .doPostParams(odata);
    }

    /** 3.1 线上线下 1-N 生成短信内容 */
    public static String online_batch_request_body(){
        return online_batch_request_body(null);
    }

    public static String online_batch_request_body(String msg){
        JSONObject odata = new JSONObject();
        if (StringUtils.isNotEmpty(msg)) {
            String[] content = msg.split("|");
            odata.put("mobile", content[0]);
            odata.put("content", content[1]);
            odata.put("msgId", ONLINE_MSGID);
            odata.put("extCode", "xxxxxxxx");
        } else {
            odata.put("content", "xxxxxxxxxxxx");
            //odata.put("mobile", phone(number));
            odata.put("mobile", "15010254294");
            odata.put("msgId", ONLINE_MSGID);
            odata.put("extCode", "xxxxxxxx");
        }
        return odata.toString();
    }
}
